package com.gzyj.server.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzyj.data.common.VehicleIncrement;
import com.gzyj.data.jtt808.alarm.Kafka_VehicleRemindContent;
import com.gzyj.data.jtt808.alarm.RemindContent;
import com.gzyj.jtt808.v2013.HeaderEncryptMethod;
import com.gzyj.jtt808.v2013.HeaderMsgBodyProperty;
import com.gzyj.jtt808.v2013.protocol.JTT808_0x8300;
import com.gzyj.pojo.set.TsSafeAlarmSet;
import com.gzyj.server.config.LogConfig;
import com.gzyj.server.factory.jtt808.NetWorkSerrvice;
import com.gzyj.server.mapper.TsSafeAlarmSetMapper;
import com.gzyj.server.utils.StackUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ZdaqService {

    @Autowired
    private TsSafeAlarmSetMapper tsSafeAlarmSetMapper;

    private TsSafeAlarmSet getVehicleForSafeTtsDic(String vehId) {
        QueryWrapper<TsSafeAlarmSet> w = new QueryWrapper<>();
        w.eq("vehid", vehId);

        return tsSafeAlarmSetMapper.selectOne(w);
    }

    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;

    public void DisposeTTSSuBiao(VehicleIncrement vehicle, com.gzyj.jtt808.v2011.protocol.subiao.JTT808_0x0200_0x64 jT808_2011_0X0200_0X64) {
        try {
            TsSafeAlarmSet safeAlarmSet = getVehicleForSafeTtsDic(vehicle.getId());
            if (safeAlarmSet == null) {
                return;
            }
            if (safeAlarmSet.getIsenable() == 0) {
                return;
            }
            RemindContent remindContent = new RemindContent() {
                {
                    VehicleId = vehicle.getId();
                    Ipaddress = vehicle.getZongduanid();
                    VehicleNo = vehicle.getCheliangpaizhao();
                    PlateColor = vehicle.getChepaiyanse();
                    RemindNote = "";
                    RemindType = 0;
                    RemindTime = new Date();
                    MessageType = "主动安全提醒";
                    AlarmNote = "主动安全提醒";
                    GpsTime = jT808_2011_0X0200_0X64.AlarmTime;
                    LastSpeed = jT808_2011_0X0200_0X64.Speed;
                }

            };
            switch (jT808_2011_0X0200_0X64.AlarmOrEventTypeString) {
                case "实线变道报警":
                    //苏表没有
                    remindContent.RemindNote = safeAlarmSet.getShixianbd();
                    remindContent.MessageType = jT808_2011_0X0200_0X64.AlarmOrEventTypeString;
                    break;
                case "前向碰撞报警":
                    remindContent.RemindNote = safeAlarmSet.getXiangqianpz();
                    remindContent.MessageType = jT808_2011_0X0200_0X64.AlarmOrEventTypeString;
                    break;
                case "车道偏离报警":
                    remindContent.RemindNote = safeAlarmSet.getChedaopl();
                    remindContent.MessageType = jT808_2011_0X0200_0X64.AlarmOrEventTypeString;
                    break;
                case "车距过近报警":
                    remindContent.RemindNote = safeAlarmSet.getChejugj();
                    remindContent.MessageType = jT808_2011_0X0200_0X64.AlarmOrEventTypeString;
                    break;
                case "行人碰撞报警":
                    remindContent.RemindNote = safeAlarmSet.getXingrenpz();
                    remindContent.MessageType = jT808_2011_0X0200_0X64.AlarmOrEventTypeString;
                    break;
                case "频繁变道报警":
                    remindContent.RemindNote = safeAlarmSet.getPinfanbd();
                    remindContent.MessageType = jT808_2011_0X0200_0X64.AlarmOrEventTypeString;
                    break;
                case "道路标识超限报警":
                    remindContent.RemindNote = safeAlarmSet.getDaolubscx();
                    remindContent.MessageType = jT808_2011_0X0200_0X64.AlarmOrEventTypeString;
                    break;
                case "障碍物报警":
                    remindContent.RemindNote = safeAlarmSet.getZhangaiwu();
                    remindContent.MessageType = jT808_2011_0X0200_0X64.AlarmOrEventTypeString;
                    break;
                default:
                    break;
            }
            if (StringUtils.isNotEmpty(remindContent.RemindNote)) {
                SendRemindToVehicle(remindContent, vehicle);
                SendRemindToKafka(remindContent);
            }
        } catch (Exception ex) {
            LogConfig.zdaqErrorLog.error("主动安全 ADAS 苏标 处理错误 \n {}", StackUtils.getStackInfo(ex));
        }
    }

    public void DisposeTTSSuBiao(VehicleIncrement vehicle, com.gzyj.jtt808.v2011.protocol.subiao.JTT808_0x0200_0x65 jT808_2011_0X0200_0X65) {
        try {
            TsSafeAlarmSet safeAlarmSet = getVehicleForSafeTtsDic(vehicle.getId());
            if (safeAlarmSet == null) {
                return;
            }
            if (safeAlarmSet.getIsenable() == 0) {
                return;
            }
            RemindContent remindContent = new RemindContent() {
                {
                    VehicleId = vehicle.getId();
                    Ipaddress = vehicle.getZongduanid();
                    VehicleNo = vehicle.getCheliangpaizhao();
                    PlateColor = vehicle.getChepaiyanse();
                    RemindNote = "";
                    RemindType = 0;
                    RemindTime = new Date();
                    MessageType = "主动安全提醒";
                    AlarmNote = "主动安全提醒";
                    GpsTime = jT808_2011_0X0200_0X65.AlarmTime;
                    LastSpeed = jT808_2011_0X0200_0X65.Speed;
                }

            };
            switch (jT808_2011_0X0200_0X65.AlarmOrEventTypeString) {
                case "接打手持电话报警":
                    remindContent.RemindNote = safeAlarmSet.getDadianhua();
                    remindContent.MessageType = jT808_2011_0X0200_0X65.AlarmOrEventTypeString;
                    break;

                case "抽烟报警":
                    remindContent.RemindNote = safeAlarmSet.getChouyan();
                    remindContent.MessageType = jT808_2011_0X0200_0X65.AlarmOrEventTypeString;
                    break;
                case "疲劳驾驶报警":
                    remindContent.RemindNote = safeAlarmSet.getPilao();
                    remindContent.MessageType = jT808_2011_0X0200_0X65.AlarmOrEventTypeString;
                    break;
                case "未检测到驾驶员报警":
                    remindContent.RemindNote = safeAlarmSet.getJiashiyuanyc();
                    remindContent.MessageType = jT808_2011_0X0200_0X65.AlarmOrEventTypeString;
                    break;
                case "驾驶员行为监测功能失效报警":
                    remindContent.RemindNote = safeAlarmSet.getZhedangsxt();
                    remindContent.MessageType = jT808_2011_0X0200_0X65.AlarmOrEventTypeString;
                    break;
                case "双手同时脱离方向盘报警":
                    remindContent.RemindNote = safeAlarmSet.getShuangtuoba();
                    remindContent.MessageType = jT808_2011_0X0200_0X65.AlarmOrEventTypeString;
                    break;
                case "长时间不目视前方报警":
                    remindContent.RemindNote = safeAlarmSet.getFenshen();
                    remindContent.MessageType = jT808_2011_0X0200_0X65.AlarmOrEventTypeString;
                    break;
                case "超时驾驶报警":
                    remindContent.RemindNote = safeAlarmSet.getChaoshijs();
                    remindContent.MessageType = jT808_2011_0X0200_0X65.AlarmOrEventTypeString;
                    break;
                default:
                    break;
            }

            if (StringUtils.isNotEmpty(remindContent.RemindNote)) {
                SendRemindToVehicle(remindContent, vehicle);
                SendRemindToKafka(remindContent);
            }
        } catch (Exception ex) {
            LogConfig.zdaqErrorLog.error("主动安全 DSM 苏标 处理错误 \n {}", StackUtils.getStackInfo(ex));
        }
    }


    @Autowired
    private NetWorkSerrvice netWorkSerrvice;

    private void SendRemindToVehicle(RemindContent remindContent, VehicleIncrement vehicle) {
        try {

            if (vehicle.getTerminalprotocoltype().equalsIgnoreCase("2019")) {
                com.gzyj.jtt808.v2019.protocol.JTT808_0x8300 jtt808_0x8300 = new com.gzyj.jtt808.v2019.protocol.JTT808_0x8300();
                com.gzyj.jtt808.v2019.HeaderMsgBodyProperty headerMsgBodyProperty = new com.gzyj.jtt808.v2019.HeaderMsgBodyProperty();
                headerMsgBodyProperty.setSubcontract(false);
                headerMsgBodyProperty.setEncryptMethod(com.gzyj.jtt808.v2019.HeaderEncryptMethod.None);
                headerMsgBodyProperty.setVersionFlag(true);
                jtt808_0x8300.getPackageHeader().setMsgBodyProperty(headerMsgBodyProperty);
                jtt808_0x8300.getPackageHeader().setTerminalPhoneNo(remindContent.getIpaddress());
                jtt808_0x8300.getPackageHeader().setProtocolVersion((byte) 1);

                jtt808_0x8300.setTextFlag((byte) 12);
                jtt808_0x8300.setTextType((byte) 1);
                jtt808_0x8300.setTextInfo(remindContent.getRemindNote());
                netWorkSerrvice.send2Veh(remindContent.getIpaddress(), jtt808_0x8300.encode());
            } else {
                JTT808_0x8300 jtt808_0x8300 = new JTT808_0x8300();

                HeaderMsgBodyProperty headerMsgBodyProperty = new HeaderMsgBodyProperty();
                headerMsgBodyProperty.setSubcontract(false);
                headerMsgBodyProperty.setEncryptMethod(HeaderEncryptMethod.None);
                jtt808_0x8300.getPackageHeader().setMsgBodyProperty(headerMsgBodyProperty);
                jtt808_0x8300.getPackageHeader().setManualMsgNum((short) 1);
                jtt808_0x8300.getPackageHeader().setTerminalPhoneNo(remindContent.getIpaddress());

                jtt808_0x8300.setTextFlagVal(12);
                jtt808_0x8300.setTextInfo(remindContent.getRemindNote());
                netWorkSerrvice.send2Veh(remindContent.getIpaddress(), jtt808_0x8300.encode());
            }


            String note = String.format("发送信息到车,信息 %s", remindContent.RemindNote);
            LogConfig.zdaqLog.info(note);

        } catch (Exception ex) {
            LogConfig.zdaqErrorLog.error(StackUtils.getStackInfo(ex));
        }
    }

    @Value("${kafka.queue.RemindContent}")
    private String Kafka_RemindContent;

    private void SendRemindToKafka(RemindContent remindContent) {
        try {
            Kafka_VehicleRemindContent content = new Kafka_VehicleRemindContent() {
                {
                    VehId = remindContent.VehicleId;
                    VehNo = remindContent.VehicleNo;
                    PlateColor = remindContent.PlateColor;
                    RemindNote = remindContent.RemindNote;
                    RemindTime = new Date();
                    RemindType = remindContent.RemindType;
                    MessageType = remindContent.MessageType;
                    AlarmNote = remindContent.AlarmNote;
                    AlarmGuid = remindContent.AlarmGuid;
                    GpsTime = remindContent.GpsTime;
                    LastSpeed = remindContent.LastSpeed;
                }

            };
            kafkaTemplate.send(Kafka_RemindContent,"Kafka_RemindContent", JSON.toJSONString(content, SerializerFeature.DisableCircularReferenceDetect));
            String note = String.format("发送信息到KAFKA,信息 %s", remindContent.RemindNote);
            LogConfig.zdaqLog.info(note);
        } catch (Exception ex) {
            LogConfig.zdaqErrorLog.error(StackUtils.getStackInfo(ex));
        }
    }


}
